
package com.magicalcoder.youyamvc.app.goodsspeccategory.service.impl;

import com.magicalcoder.youyamvc.app.goodsspeccategory.dao.GoodsSpecCategoryDao;
import com.magicalcoder.youyamvc.app.goodsspeccategory.service.GoodsSpecCategoryService;
import com.magicalcoder.youyamvc.app.model.GoodsSpecCategory;
import com.magicalcoder.youyamvc.core.common.utils.ListUtils;
import com.magicalcoder.youyamvc.core.common.utils.ProjectUtil;
import com.magicalcoder.youyamvc.core.common.utils.StringUtils;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.magicalcoder.youyamvc.core.common.utils.copy.Copyer;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

/**
* 代码为自动生成 Created by www.magicalcoder.com
* 如果你改变了此类 read 请将此行删除
*  欢迎加入官方QQ群:323237052
*/

@Component("goodsSpecCategoryService")
public class GoodsSpecCategoryServiceImpl implements GoodsSpecCategoryService{
    @Resource(name="goodsSpecCategoryDao")
    private GoodsSpecCategoryDao goodsSpecCategoryDao;


    @Override
    public GoodsSpecCategory getGoodsSpecCategory(Long id) {
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("id", id);
        return goodsSpecCategoryDao.getGoodsSpecCategory(query);
    }

    @Override
    public GoodsSpecCategory selectOneGoodsSpecCategoryWillThrowException(Map<String, Object> query){
        return goodsSpecCategoryDao.getGoodsSpecCategory(query);
    }

    @Override
    public GoodsSpecCategory selectFirstGoodsSpecCategory(Map<String, Object> query){
        query.put("limit",1);
        List<GoodsSpecCategory> list = goodsSpecCategoryDao.getGoodsSpecCategoryList(query);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<GoodsSpecCategory> getGoodsSpecCategoryList(Map<String, Object> query) {
        return goodsSpecCategoryDao.getGoodsSpecCategoryList(query);
    }

    @Override
    public Integer getGoodsSpecCategoryListCount(Map<String, Object> query) {
        return goodsSpecCategoryDao.getGoodsSpecCategoryListCount(query);
    }

    @Override
    public     Long  insertGoodsSpecCategory(GoodsSpecCategory entity) {
        return goodsSpecCategoryDao.insertGoodsSpecCategory(entity);
    }

    @Override
    public void updateGoodsSpecCategory(GoodsSpecCategory entity) {
        //校验
        goodsSpecCategoryDao.updateGoodsSpecCategory(entity);
    }

    @Override
    public void updateGoodsSpecCategoryWithoutNull(GoodsSpecCategory entity) {
        //校验
        goodsSpecCategoryDao.updateGoodsSpecCategoryWithoutNull(entity);
    }

    @Override
    public void updateGoodsSpecCategoryByWhereSql(Map<String,Object> entity,String whereSql) {
        if(entity==null ||entity.isEmpty()){
            throw new RuntimeException("entity不能为空");
        }
        if(StringUtils.isBlank(whereSql)){
            throw new RuntimeException("whereSql不能为空");
        }
        entity.put("whereSql",whereSql);
        //校验
        goodsSpecCategoryDao.updateGoodsSpecCategoryByWhereSql(entity);
    }

    @Override
    public void deleteGoodsSpecCategory(Long id) {
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("id", id);
        goodsSpecCategoryDao.deleteGoodsSpecCategory(query);
    }
    @Override
    public void deleteGoodsSpecCategoryList(Map<String,Object> entity){
        goodsSpecCategoryDao.deleteGoodsSpecCategoryList(entity);
    }

    @Override
    public void deleteGoodsSpecCategoryByWhereSql(String whereSql) {
        if(StringUtils.isBlank(whereSql)){
            throw new RuntimeException("whereSql不能为空");
        }
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("whereSql", whereSql);
        goodsSpecCategoryDao.deleteGoodsSpecCategoryByWhereSql(query);
    }



    @Override
    public void truncateGoodsSpecCategory() {
        goodsSpecCategoryDao.truncateGoodsSpecCategory();
    }

    @Override
    public void batchInsertGoodsSpecCategory(List<GoodsSpecCategory> list) {
        //校验
        goodsSpecCategoryDao.batchInsertGoodsSpecCategory(list);
    }

    @Override
    public void batchUpdateGoodsSpecCategory(List<GoodsSpecCategory> list) {
        //校验
        goodsSpecCategoryDao.batchUpdateGoodsSpecCategory(list);
    }
    @Override
    public void batchDeleteGoodsSpecCategory(List<Long> idList) {
        goodsSpecCategoryDao.batchDeleteGoodsSpecCategory(idList);
    }

    @Override
    public void batchDeleteGoodsSpecCategoryList(List<GoodsSpecCategory> entityList){
        goodsSpecCategoryDao.batchDeleteGoodsSpecCategoryList(entityList);
    }

    @Transactional
    @Override
    public void transactionImportJsonList(List<GoodsSpecCategory> list) {
        if(list!=null && list.size()>0){
            for(GoodsSpecCategory goodsSpecCategory : list){
                if (goodsSpecCategory.getId() == null) {
                    insertGoodsSpecCategory(goodsSpecCategory);
                } else {
                    GoodsSpecCategory entity = getGoodsSpecCategory(goodsSpecCategory.getId());
                    if(entity==null){
                        insertGoodsSpecCategory(goodsSpecCategory);
                    }else {
                        Copyer.copy(goodsSpecCategory, entity);
                        updateGoodsSpecCategory(entity);
                    }
                }
            }
        }
    }


    public List<GoodsSpecCategory> tryQueryList(String keyword,String selectValue, String foreignJavaField){
        List<GoodsSpecCategory> list;
        if(StringUtils.isBlank(keyword) || "全部".equals(keyword)){
            return getGoodsSpecCategoryList(ProjectUtil.buildMap("limitIndex",0,"limit", 20));
        }else{
            list = searchList("nameFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            list = searchList("statusFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
        }
        return new ArrayList<GoodsSpecCategory>();
    }

    private List<GoodsSpecCategory> searchList(String field,String keyword){
        List<GoodsSpecCategory> list = getGoodsSpecCategoryList(ProjectUtil.buildMap(field,keyword,"limitIndex",0,"limit", 20));
        if(ListUtils.isNotBlank(list)){
            return list;
        }
        String[] keys = keyword.split("-");
        for(String key:keys){
            list = getGoodsSpecCategoryList(ProjectUtil.buildMap(field,key,"limitIndex",0,"limit", 20));
            if(ListUtils.isNotBlank(list)){
                return list;
            }
        }
        return null;
    }
}
